//
//  ImageMenuBarView.m
//  ImageEnginePlugin
//
//  Created by VNG on 10/18/12.
//
//

#import "ImageMenuBarView.h"
#include "Utils.h"

@implementation ImageMenuBarView

@synthesize numButton;
@synthesize indexSelectIndex;

-(void)buttonSelect:(id)sender
{
    if (indexSelectIndex >= 0) {
        if (!hasEffectOut) {
            UIButton* btn = (UIButton *)[[self subviews] objectAtIndex:indexSelectIndex];
            if ([btn isKindOfClass:[UIButton class]]) {
                [[btn layer] setBorderWidth:1.0];
                [[btn layer] setBorderColor:UIColorFromRGB(0x666666).CGColor];
            }
        }
    }
    
    UIButton* button = (UIButton *)sender;
    if (([button isKindOfClass:[UIButton class]]) &&(button.tag >= 0)) {
        indexSelectIndex = button.tag;
        
        if (!hasEffectOut) {
            [[button layer] setBorderWidth:2.0];
            [[button layer] setBorderColor:UIColorFromRGB(0x21BAF0).CGColor];
        }
        
        if (hasEffectOut) {
            [self effectTranslationOut];
        }else{
            if ([menuBarDelegate respondsToSelector:@selector(finishedAnimationWhenTouchButton:)])
                [menuBarDelegate finishedAnimationWhenTouchButton:indexSelectIndex];
        }
    }
}

-(void)effectTranslationOut
{
    if (!hasEffectOut) {
        return;
    }
    CGRect rect = self.frame;
    rect.origin.x = -rect.size.width;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4f];
    [UIView setAnimationDelegate:self];
    
    self.frame = rect;

    [UIView commitAnimations];
    
    if ([menuBarDelegate respondsToSelector:@selector(beginAnimationWhenTouchDown:)]) {
        [menuBarDelegate beginAnimationWhenTouchDown:indexSelectIndex];
    }
}

-(void)effectTranslationIn
{
    CGRect rect = self.frame;
    rect.origin.x = 0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4f];
    self.frame = rect;
    [UIView commitAnimations];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if (!hasEffectOut) {
        return;
    }
    if ([menuBarDelegate respondsToSelector:@selector(finishedAnimationWhenTouchButton:)])
        [menuBarDelegate finishedAnimationWhenTouchButton:indexSelectIndex];
}

-(void)resetSelectedIndex
{
    if (indexSelectIndex >= 0) {
        if (!hasEffectOut) {
            UIButton* btn = (UIButton *)[[self subviews] objectAtIndex:indexSelectIndex];
            if ([btn isKindOfClass:[UIButton class]]) {
                [[btn layer] setBorderWidth:1.0];
                [[btn layer] setBorderColor:UIColorFromRGB(0x666666).CGColor];
            }
        }
        indexSelectIndex = -1;
    }
}

-(void)setSelectIndex:(int)index
{
    [self resetSelectedIndex];
    
    indexSelectIndex = index;
    if (!hasEffectOut) {
        UIButton* btn = (UIButton *)[[self subviews] objectAtIndex:indexSelectIndex];
        if ([btn isKindOfClass:[UIButton class]]) {
            [[btn layer] setBorderWidth:2.0];
            [[btn layer] setBorderColor:UIColorFromRGB(0x21BAF0).CGColor];
        }
    }
}

-(void)refreshBar
{
    
}

-(void)progessFinishedDownload
{
    
}

-(void)showMenu
{
    
}

-(void)hiddenMenu
{
    
}

-(NSString *)getStringLayerAtButtonSelected
{
    return nil;
}

-(NSString *)getStringLayerAtButtonIndex:(NSInteger)index
{
    return nil;
}

- (void)dealloc
{
    
    
    [super dealloc];
}
@end
